<template>
  <div class="login-container">
    <van-nav-bar title="登录">
      <template #left>
        <img class="logo" src="@/assets/images/logo.png" />
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        type="tel"
        name="手机号"
        label="手机号"
        required
        placeholder="手机号"
        :rules="rules.tel"
      />
      <van-field
        v-model="formLogin.code"
        type="text"
        name="密码"
        label="密码"
        placeholder="密码"
        required
        :rules="rules.password"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="加载中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { mapActions } from 'vuex'
// import router from '../router'
export default {
  name: 'Login',
  data () {
    return {
      isLoading: false,
      formLogin: {
        mobile: '15128013042',
        code: '246810'
      },
      rules: {
        tel: [
          { required: true, message: '请填写手机号', trigger: 'onChange' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请填写正确的手机号',
            trigger: 'onChange'
          }
        ],

        password: [
          { required: true, message: '请填写密码', trigger: 'onChange' },
          {
            pattern: /\d{6}$/,
            message: '请填写正确的密码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['asyncLoadingAction']),
    async onSubmit () {
      this.isLoading = true
      try {
        await this.asyncLoadingAction(this.formLogin)
        Notify({ type: 'success', message: '登录成功' })
      } catch (err) {
        console.error(err)
      } finally {
        this.isLoading = false
      }
      this.$router.replace({ path: this.$route.query.from || '/layout' })
    }
  }
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
</style>
